package org.jeecg.modules.dafen.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.dafen.model.CnDafenresult;
import org.jeecg.modules.dafen.model.Static;
import org.jeecg.modules.dafen.service.CnDafenresultService;
import org.jeecg.modules.dd.model.CnDaka;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dd")
public class StaticController {

    @Autowired
    private CnDafenresultService cnDafenresultService;

    //统计
//    @RequestMapping("/static")
    public ArrayList<Static> statics() {
//        cnDafenresultService.statics();
        LambdaQueryWrapper<CnDafenresult> cnDafenresultLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //安装ratee分组
        List<CnDafenresult> list = cnDafenresultService.list();
        //steream流按照ratee分组
        Map<String, List<CnDafenresult>> collect = list.stream().collect(Collectors.groupingBy(CnDafenresult::getRatee));
        //total进行排序
        collect.forEach((k, v) -> {
            v.sort((o1, o2) -> o2.getTotal() - o1.getTotal());
        });
        //如果大于两个，删除第一个和最后一个
        collect.forEach((k, v) -> {
            if (v.size() > 2) {
                v.remove(0);
                v.remove(v.size() - 1);
            }
        });
        //计算平均值，放在一个新的map中，这个map重新new，保留两位小数
        ArrayList<Static> staticsList = new ArrayList<>();
//        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        collect.forEach((k, v) -> {
            double sum = 0;
            for (CnDafenresult cnDafenresult : v) {
                sum += cnDafenresult.getTotal();
            }
            double avg = sum / v.size();
            Static statics = new Static();
            statics.setName(k);
            statics.setValue(String.valueOf(avg));
            staticsList.add(statics);
//            stringObjectHashMap.put(k, String.format("%.2f", avg));
        });
        return staticsList;

    }

    @GetMapping(value = "/daFenExport")
    public ModelAndView exportXls(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        ArrayList<Static> statics = statics();
        //导出文件名称
        mv.addObject(NormalExcelConstants.FILE_NAME, "创尼电子科技技术有限公司考核表");
        //注解对象Class
        mv.addObject(NormalExcelConstants.CLASS, Static.class);
        //自定义导出字段 		   mv.addObject(NormalExcelConstants.EXPORT_FIELDS,"name,keyWord,punchTime");
        //自定义表格参数
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("创尼电子科技技术有限公司考核表", "创尼电子科技技术有限公司考核表"));
        //导出数据列表
        mv.addObject(NormalExcelConstants.DATA_LIST, statics);
        return mv;
    }
}
